const express = require("express");
const Users=require("../models/Reg");
const db = require("../db/index");
const router = new express.Router();
router.use("/",async(req,res,next)=>{
    await db;
    next();
})
router.post("/reg", async(req, res) => {
    const { username, password } = req.body;
    const username_reg=/^[A-z0-9]{3,12}$/;
    const password_reg=/^[A-z0-9]{3,12}$/;
    if (username_reg.test(username.trim()) && password_reg.test(password.trim())) {
        const result_find = await Users.findOne({
            username
        });
        console.log(result_find);
        if (result_find) {
            res.send('用户名已存在');
        } else {
            const result = await Users.create({
                username,
                password
            });
            res.send('注册成功');
        }
    }else{
        res.send("注册失败")
    }
})
module.exports = router;